from flask import Flask, request, jsonify
from flask_cors import CORS

from model import mylist
from zone import Auth

app = Flask(__name__)
CORS(app)
access_key = 'AJLzx4IfvsFEFslZjazhVnsf9yJNtwiwouavw8vk'
secret_key = 'PRr3fVwZ-0IBAFyW2rOPsKV1X3AJAUruDWsZq7EZ'
bucket_name = 'Bucket_Name'
q = Auth(access_key, secret_key)
myList = mylist.mylist()


@app.route('/')
def hello_world():
    return jsonify(myList.find({})), 200


@app.route('/myList/', methods=['GET'])
def get_myLists():
    return jsonify(myList.find({})), 200


@app.route('/myList/<string:myList_id>', methods=['GET'])
def get_myList(myList_id):
    return myList.find_by_id(myList_id), 200


@app.route('/myList/', methods=['POST'])
def add_myList():
    if request.method == "POST":
        json_data = request.get_json()
        name = json_data['name']
        alexa = json_data['alexa']
        url = json_data['url']
        response = myList.create({'name': name, 'alexa': alexa, 'url': url})
        return response, 201


@app.route('/myList/<string:myList_id>/', methods=['PUT'])
def update_myList(myList_id):
    if request.method == "PUT":
        json_data = request.get_json()
        name = json_data['name']
        alexa = json_data['alexa']
        url = json_data['url']
        response = myList.update(myList_id, {'name': name, 'alexa': alexa, 'url': url})
        return response, 201


@app.route('/myList/<string:myList_id>/', methods=['DELETE'])
def delete_tasks(myList_id):
    if request.method == "DELETE":
        myList.delete(myList_id)
        return "Record Deleted"


if __name__ == '__main__':
    app.run(debug=type)
